package com.guldan.core.utils;

import java.math.BigDecimal;
import java.util.regex.Pattern;

/**
 * Created by Silas.
 * Date: 2016/12/30
 * Time: 16:44
 */
public class MathUtil {

    //判断是否是整型
    public static boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }


    //判断是否是数字+字母
    public static boolean isNumberAndChar(String str) {
        Pattern pattern = Pattern.compile("[\\w]*$");
        return pattern.matcher(str).matches();
    }



    //判断是否是double or flase
    public static boolean isDouble(String str) {
        Pattern pattern = Pattern.compile("^[-\\+]?[.\\d]*$");
        return pattern.matcher(str).matches();
    }


    public static BigDecimal getDecimalV(BigDecimal args1) {
        return args1 == null ? BigDecimal.ZERO : args1;
    }

    public static int getIntV(Integer args1) {
        return args1 == null ? 0 : args1;
    }

    public static long getLongV(Long args1) {
        return args1 == null ? 0 : args1;
    }

    public static double getDoubleV(Double args1) {
        return args1 == null ? 0 : args1;
    }

    public static int plus(Integer args1, Integer args2) {
        return getIntV(args1) + getIntV(args2);
    }

    public static long plus(Long args1, Long args2) {
        return getLongV(args1) + getLongV(args2);
    }

    public static BigDecimal plus(BigDecimal args1, BigDecimal args2) {
        return getDecimalV(args1).add(getDecimalV(args2));
    }

    public static BigDecimal divide(BigDecimal args1, Integer num, int scale, int roundingMode) {
        if (getIntV(num) == 0) return BigDecimal.ZERO;
        return getDecimalV(args1).divide(BigDecimal.valueOf(num), scale, roundingMode);
    }


    public static Integer minus(Integer args1, Integer args2) {
        return (args1 == null ? 0 : args1) - (args2 == null ? 0 : args2);
    }

    public static void main(String[] args) {

        BigDecimal r = MathUtil.divide(new BigDecimal(4), 7, 2, BigDecimal.ROUND_HALF_UP);
        System.out.println(r.doubleValue());


        System.out.println(isInteger("-1"));
        System.out.println(isInteger("10"));
        System.out.println(isInteger("01"));
        System.out.println(isInteger("1.2"));
        System.out.println(isInteger("-1.3"));

        System.out.println("");

        System.out.println(isDouble("-1"));
        System.out.println(isDouble("10"));
        System.out.println(isDouble("01"));
        System.out.println(isDouble("1.2"));
        System.out.println(isDouble("-1.3"));

        System.out.println("");

        System.out.println(divide(new BigDecimal(10), 100, 3, BigDecimal.ROUND_HALF_UP));

        System.out.println("");

        System.out.println(isNumberAndChar("333xxx"));
        System.out.println(isNumberAndChar("x333xxx"));
        System.out.println(isNumberAndChar("-33xxx"));
        System.out.println(isNumberAndChar("333+xxx"));
        System.out.println(isNumberAndChar("333.xxx"));
        System.out.println(isNumberAndChar("33333444"));
        System.out.println(isNumberAndChar("retretrete"));

    }

}
